package com.xiaoma.seckill.scheduled;

import com.xiaoma.seckill.service.SeckillService;
import lombok.extern.slf4j.Slf4j;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @Author: xiaoma
 * @Date: 2021/7/26 18:00
 * @description:
 */
@EnableScheduling
@EnableAsync
@Service
@Slf4j
public class SeckillScheduled {

    @Autowired
    private SeckillService seckillService;
    @Autowired
    private RedissonClient redissonClient;

    private final String lock ="seckill:upload:lock";
   /**
    * @Description  库存预热，定时上架3天内秒杀商品 每天10点执行一次
    * @Author Xiaoma
    * @Date 2021/8/20 17:47
    * @param
    * @return void
    **/
//    @Scheduled(cron = "0 * * * * ?")
    @Scheduled(cron = "0 0 10 * * ?")
    @Async
    public void demo() throws InterruptedException {
        RLock lock = redissonClient.getLock(this.lock);
        lock.lock(10, TimeUnit.SECONDS);
        try {
            log.info("定时任务上架秒杀商品信息");
            seckillService.getLate3DaySeckill();
        }
        finally {
            lock.unlock();
        }
    }

}
